﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using AcadPosApp.Utilidades;

namespace AcadPosApp.Seguranca
{
    public partial class UsuarioXRegras : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CarregarRegras();
                MembershipUserCollection list = Membership.GetAllUsers();
                gvwListUsuarios.DataSource = list;
                gvwListUsuarios.DataBind();
            }
        }

        private void CarregarUsuariosDaRegra()
        {
            string[] usersInRole = Roles.GetUsersInRole(HttpUtility.HtmlDecode(gvwRegras.Rows[gvwRegras.SelectedIndex].Cells[2].Text));
            MembershipUserCollection users = new MembershipUserCollection();
            foreach (string userName in usersInRole)
            {
                users.Add(Membership.GetUser(userName));
            }
            gvwUsuarios.DataSource = users;
            gvwUsuarios.DataBind();
        }

        private void CarregarRegras()
        {
            gvwRegras.DataSource = Roles.GetAllRoles();
            gvwRegras.DataBind();
        }

        protected void gvwRegras_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string msg = string.Empty;
            try
            {
                //validar se existe algum usuario pra determinada regra
                if (Roles.DeleteRole(gvwRegras.Rows[e.RowIndex].Cells[2].Text))
                {
                    ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Alert", string.Format("alert('{0}');", Alertas.obterMsg((int)EnumAcadPos.Mensagem.SucessoExcluir, "")), true);                        
                    CarregarRegras();
                }
            }
            catch (System.Exception)
            {
                msg = "Regra não pode ser deletada, pois existem usuário(s) vinculado.";
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Alert", string.Format("alert('{0}');", msg), true);
            }            
        }

        protected void gvwRegras_SelectedIndexChanged(object sender, EventArgs e)
        {
            CarregarUsuariosDaRegra();
        }

        protected void gvwUsuarios_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string msg = string.Empty;
            if (Roles.IsUserInRole(gvwUsuarios.Rows[e.RowIndex].Cells[1].Text, gvwRegras.Rows[gvwRegras.SelectedIndex].Cells[2].Text))
            {
                Roles.RemoveUserFromRole(gvwUsuarios.Rows[e.RowIndex].Cells[1].Text, gvwRegras.Rows[gvwRegras.SelectedIndex].Cells[2].Text);
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Alert", string.Format("alert('{0}');", msg), true);
                CarregarUsuariosDaRegra();
            }                
        }

        protected void gvwListUsuarios_SelectedIndexChanged(object sender, EventArgs e)
        {
            string msg = string.Empty;
            try
            {
                Roles.AddUserToRole(gvwListUsuarios.Rows[gvwListUsuarios.SelectedIndex].Cells[1].Text, gvwRegras.Rows[gvwRegras.SelectedIndex].Cells[2].Text);
                CarregarUsuariosDaRegra();
                msg = "Regra vinculada a usuário!";
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Alert", string.Format("alert('{0}');", msg), true);
            }
            catch (System.Exception)
            {
                msg = "Regra não pôde ser vinculada a usuário!";
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Alert", string.Format("alert('{0}');", msg), true);
            }

            if (!string.IsNullOrEmpty(msg))
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Alert", string.Format("alert('{0}');", msg), true);
        }
    }
}